package com.langyiquan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.langyiquan.mapper.ItemCategoryMapper;
import com.langyiquan.mapper.custom.CategoryCustomMapper;
import com.langyiquan.pojo.ItemCategory;
import com.langyiquan.service.CategoryService;
import com.langyiquan.utils.PagedGridResult;
import com.langyiquan.vo.CommonItemsVO;
import com.langyiquan.vo.SimpleItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private ItemCategoryMapper itemCategoryMapper;

    @Autowired
    private CategoryCustomMapper categoryCustomMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemCategory> queryHotCat() {
        Example example = new Example(ItemCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId", 0);
        criteria.andEqualTo("isHot", 1);
        List<ItemCategory> result = itemCategoryMapper.selectByExample(example);
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemCategory> getSubCatList(Integer rootCatId) {
        Example example = new Example(ItemCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId", rootCatId);
        List<ItemCategory> result = itemCategoryMapper.selectByExample(example);
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedGridResult getItemsByCatId(Integer catId,Integer catType, String sort,Integer page, Integer pageSize) {

        Map<String,Object> map= new HashMap<>();
        map.put("catId",catId);
        map.put("catType",catType);
        map.put("sort",sort);
        
        PageHelper.startPage(page,pageSize);
        List<CommonItemsVO> list = categoryCustomMapper.getItemsByCatId(map);

        PageInfo<?> pageList = new PageInfo<>(list);
        PagedGridResult grid = new PagedGridResult();
        grid.setPage(page);
        grid.setRows(list);
        grid.setTotal(pageList.getPages());
        grid.setRecords(pageList.getTotal());

        return grid;

    }
}
